package com.lwt.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwt.gulimall.common.utils.R;
import com.lwt.gulimall.product.entity.Category;
import com.lwt.gulimall.product.service.CategoryService;
import com.lwt.gulimall.product.vo.CategoryVo;
import com.lwt.gulimall.product.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 商品三级分类
 *
 * @author liuweitao
 * @email liuweitao2541@gmail.com
 * @date 2024-01-26 16:11:42
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        long pageSize = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<Category> page = new Page<>(pageSize, limit);
        Page<Category> pageResult = categoryService.page(page, null);

        return R.ok().put("page", pageResult);
    }

    @PostMapping("/list/test")
    public R lists(@RequestBody PageVo params) {

        long pageSize = Long.parseLong(params.getPage());
        long limit = Long.parseLong(params.getLimit());
        Page<Category> page = new Page<>(pageSize, limit);
        Page<Category> pageResult = categoryService.page(page, null);

        return R.ok().put("page", pageResult);
    }

    @GetMapping("/list/tree")
    public R listTree() {
        return R.ok().put("collectList", categoryService.listTree());
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        Category category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Category category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody Category category) {
        categoryService.updateDetail(category);

        return R.ok();
    }

    @PostMapping("/updates")
    public R updates(@RequestBody List<Category> categoryList) {
        categoryService.updateBatchById(categoryList);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
